package info.androidhive.tabsswipe;


import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;


public class AddCardFragment extends android.support.v4.app.Fragment {

    String[] banks = {"ПриватБанк", "Укрсоцбанк(uniCredit)", "Укрэксимбанк", "Аваль", "Кредобанк", "Марфин Банк",
            "Правэкс-Банк", "Профин банк", "ПУМБ", "Укргазбанк", "Укринбанк", "УкрСиббанк"};
    Integer[] banks_icons = {R.drawable.privat, R.drawable.uni_credit, R.drawable.exim_bank, R.drawable.aval, R.drawable.kredo, R.drawable.marfin,
            R.drawable.pravex, R.drawable.profin, R.drawable.pumb, R.drawable.ukr_gas, R.drawable.ukr_in, R.drawable.ukr_sib};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        View v = inflater.inflate(R.layout.fragment_add_card, container, false);
        TextView card_number_tv = (TextView) v.findViewById(R.id.tv);
        card_number_tv.setText("Введите последние 4 цифры номера Вашей карты");
        TextView card_name_tv = (TextView) v.findViewById(R.id.tv2);
        TextView bank_tv = (TextView) v.findViewById(R.id.tv1);
        card_name_tv.setText("Введите название карты");
        bank_tv.setText("Выберите банк");
        Spinner spinner = (Spinner) v.findViewById(R.id.spinner);
        Button add_btn = (Button) v.findViewById(R.id.btn);
        add_btn.setText("Добавить");

        SpinnerAdapter adapter = new SpinnerAdapter(getActivity(),
                R.layout.spinner_row, banks);

        // Вызываем адапетр
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int pos, long id) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
        });

        return v;
    }

    public class SpinnerAdapter extends ArrayAdapter<String> {

        public SpinnerAdapter(Context context, int textViewResourceId,
                              String[] objects) {
            super(context, textViewResourceId, objects);
            // TODO Auto-generated constructor stub
        }

        @Override
        public View getDropDownView(int position, View convertView,
                                    ViewGroup parent) {
            // TODO Auto-generated method stub
            return getCustomView(position, convertView, parent);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            return getCustomView(position, convertView, parent);
        }

        public View getCustomView(int position, View convertView,
                                  ViewGroup parent) {
            // TODO Auto-generated method stub
            // return super.getView(position, convertView, parent);

            LayoutInflater inflater = getActivity().getLayoutInflater();
            View row = inflater.inflate(R.layout.spinner_row, parent, false);
            TextView label = (TextView) row.findViewById(R.id.bank);
            label.setText(banks[position]);

            ImageView icon = (ImageView) row.findViewById(R.id.icon);
            icon.setImageResource(banks_icons[position]);

            return row;
        }
    }


}
